package com.chenxin.wms.controller;

import com.chenxin.common.annotation.Log;
import com.chenxin.common.annotation.WmsLogs;
import com.chenxin.common.core.controller.BaseController;
import com.chenxin.common.core.domain.AjaxResult;
import com.chenxin.common.core.page.TableDataInfo;
import com.chenxin.common.enums.BusinessType;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.common.utils.poi.ExcelUtil;
import com.chenxin.wms.domain.SpuEntity;
import com.chenxin.wms.domain.vo.SpuVO;
import com.chenxin.wms.service.ISpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物料Controller
 *
 * @author gry
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/wms/spu")
public class SpuController extends BaseController {
    @Autowired
    private ISpuService spuService;

    /**
     * 查询物料列表
     */
    @PreAuthorize("@ss.hasPermi('wms:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpuVO spuVo) {
        startPage();
        List<SpuVO> list = spuService.selectSpuList(spuVo);
        return getDataTable(list);
    }

    /**
     * 导出物料列表
     */
    @PreAuthorize("@ss.hasPermi('wms:spu:export')")
    @Log(title = "物料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpuVO spuVo) {
        List<SpuVO> list = spuService.selectSpuList(spuVo);
        ExcelUtil<SpuVO> util = new ExcelUtil<>(SpuVO.class);
        util.exportExcel(response, list, "物料数据");
    }

    /**
     * 获取物料详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:spu:query')")
    @GetMapping(value = {"/", "/{spuId}"})
    public AjaxResult getInfo(@PathVariable(value = "spuId", required = false) Long spuId) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(spuId)) {
            SpuVO view = spuService.selectSpuViewById(spuId);
            ajax.put(AjaxResult.DATA_TAG, view);
        }
        //获取仓库信息、库区信息、包装信息等内容
        return ajax;
    }

    /**
     * 新增物料
     */
    @PreAuthorize("@ss.hasPermi('wms:spu:add')")
    @WmsLogs(name = "新增物料")
    @PostMapping
    public AjaxResult add(@RequestBody SpuEntity spuEntity) {
        //1.检查物料编号是否存在
        if (!spuService.checkSpuNoUnique(spuEntity)) {
            return error("新增物料失败,对应物料编号[" + spuEntity.getSpuNo() + "]已存在");
        }
        spuEntity.setCreator(getUserId());
        spuEntity.setCreateName(getUsername());
        return toAjax(spuService.insertSpu(spuEntity));
    }

    /**
     * 修改物料
     */
    @PreAuthorize("@ss.hasPermi('wms:spu:edit')")
    @WmsLogs(name = "修改物料")
    @PutMapping
    public AjaxResult edit(@RequestBody SpuEntity spuEntity) {
        // 填充修改信息
        return toAjax(spuService.updateSpu(spuEntity));
    }

    /**
     * 删除物料
     */
    @PreAuthorize("@ss.hasPermi('wms:spu:remove')")
    @Log(title = "物料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{spuIds}")
    public AjaxResult remove(@PathVariable Long[] spuIds) {
        return toAjax(spuService.deleteSpuBySpuIds(spuIds));
    }
}
